#include "createnewgroupevent.h"
#include "appchat/profilemanager.h"

CreateNewGroupEvent::CreateNewGroupEvent(const QVariant &data, QObject *parent) :
    AbstractEvent(parent)
{
    parseList(data);
}

void CreateNewGroupEvent::parseList(const QVariant &data)
{
    QVariantMap map = data.toMap();

    if (!map.contains("cuser")) {
        return;
    }
    QVariant cuserVariant = map.value("cuser");
    if (cuserVariant.type() != QVariant::String) {
        return;
    }

    if (!map.contains("id")) {
        return;
    }
    QVariant idVariant = map.value("id");
    if (idVariant.type() != QVariant::String) {
        return;
    }

    if (!map.contains("name")) {
        return;
    }
    QVariant nameVariant = map.value("name");
    if (nameVariant.type() != QVariant::String) {
        return;
    }

    if (!map.contains("fids")) {
        return;
    }
    QVariant fids = map.value("fids");
    if (fids.type() != QVariant::List) {
        return;
    }


    QStringList aListFids = fids.toStringList();

    for (int i = 0; i < aListFids.length(); i++) {
        m_listFids.append(aListFids.at(i).toLongLong());
    }

    m_groupId = idVariant.toLongLong();
    m_groupName = nameVariant.toString();
    m_countUser = cuserVariant.toInt();

}

void CreateNewGroupEvent::process()
{
    ProfileManager::instance()->createNewGroupEvent(*this);
}

AbstractEvent::EventType
CreateNewGroupEvent::eventType() const
{
    return AbstractEvent::CreateNewGroupEvent;
}
